package com.ruoyi.project.data.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.aspectj.lang.annotation.DataSource;
import com.ruoyi.framework.aspectj.lang.enums.DataSourceType;
import com.ruoyi.project.data.domain.Car;
import com.ruoyi.project.data.domain.TbTransportDriverCar;
import com.ruoyi.project.data.mapper.CarMapper;
import com.ruoyi.project.data.mapper.TbTransportDriverCarMapper;
import com.ruoyi.project.data.service.ITbTransportDriverCarService;
import com.ruoyi.project.system.domain.SysDictData;
import com.ruoyi.project.system.mapper.SysDictDataMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 人员车辆Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-03-23
 */
@Service
public class TbTransportDriverCarServiceImpl implements ITbTransportDriverCarService
{
    @Autowired
    private TbTransportDriverCarMapper tbTransportDriverCarMapper;
    @Autowired
    private SysDictDataMapper dictDataMapper;
    @Autowired
    private CarMapper carMapper;

    /**
     * 查询人员车辆
     * 
     * @param carId 人员车辆ID
     * @return 人员车辆
     */
    @Override
    public TbTransportDriverCar selectTbTransportDriverCarById(Long carId)
    {
        TbTransportDriverCar car = tbTransportDriverCarMapper.selectTbTransportDriverCarById(carId);
        if(car.getCarLicenceColor() != null){
            List<SysDictData> carColorList = dictDataMapper.selectDictDataByType("car_color");
            if(carColorList != null && carColorList.size() > 0){
                for(SysDictData dict : carColorList){
                    if(car.getCarLicenceColor().equals(dict.getDictValue())){
                        car.setCarLicenceColorName(dict.getDictLabel());
                        break;
                    }
                }
            }
        }
        if(car.getCarType() != null){
            List<SysDictData> carTypeList = dictDataMapper.selectDictDataByType("car_type");
            if(carTypeList != null && carTypeList.size() > 0){
                for(SysDictData dict : carTypeList){
                    if(car.getCarType().equals(dict.getDictValue())){
                        car.setCarTypeName(dict.getDictLabel());
                        break;
                    }
                }
            }
        }
        if("1".equals(car.getHasTrailerCar())){
            TbTransportDriverCar carg = tbTransportDriverCarMapper.selectTbTransportDriverCarById(car.getTrailerCarId());
            if(carg != null){
                car.setTrailerCarNo(carg.getCarLicenceNumber());
            }
        }
        return car;
    }

    /**
     * 查询人员车辆列表
     * 
     * @param tbTransportDriverCar 人员车辆
     * @return 人员车辆
     */
    @Override
    public List<TbTransportDriverCar> selectTbTransportDriverCarList(TbTransportDriverCar tbTransportDriverCar)
    {
        return tbTransportDriverCarMapper.selectTbTransportDriverCarList(tbTransportDriverCar);
    }

    /**
     * 新增人员车辆
     * 
     * @param tbTransportDriverCar 人员车辆
     * @return 结果
     */
    @Override
    public int insertTbTransportDriverCar(TbTransportDriverCar tbTransportDriverCar)
    {
        tbTransportDriverCar.setCreateTime(DateUtils.getNowDate());
        return tbTransportDriverCarMapper.insertTbTransportDriverCar(tbTransportDriverCar);
    }

    /**
     * 修改人员车辆
     * 
     * @param tbTransportDriverCar 人员车辆
     * @return 结果
     */
    @Override
    public int updateTbTransportDriverCar(TbTransportDriverCar tbTransportDriverCar)
    {
        tbTransportDriverCar.setUpdateTime(DateUtils.getNowDate());
        tbTransportDriverCar.setAuthenticationtype("1");
        return tbTransportDriverCarMapper.updateTbTransportDriverCar(tbTransportDriverCar);
    }

    /**
     * 批量删除人员车辆
     * 
     * @param carIds 需要删除的人员车辆ID
     * @return 结果
     */
    @Override
    public int deleteTbTransportDriverCarByIds(Long[] carIds)
    {
        return tbTransportDriverCarMapper.deleteTbTransportDriverCarByIds(carIds);
    }

    /**
     * 删除人员车辆信息
     * 
     * @param carId 人员车辆ID
     * @return 结果
     */
    @Override
    public int deleteTbTransportDriverCarById(Long carId)
    {
        return tbTransportDriverCarMapper.deleteTbTransportDriverCarById(carId);
    }

    @Override
    @DataSource(value = DataSourceType.SLAVE2)
    public Car getCar(String model) {
        return carMapper.getCar(model);
    }

    @Override
    public String getModel(String idcard, String carNo){
        return tbTransportDriverCarMapper.getCarModel(idcard, carNo);
    }

    @Override
    @Transactional
    @DataSource(value = DataSourceType.SLAVE2)
    public int insertCar(Car car) {
        return carMapper.insertCar(car);
    }


    @DataSource(value = DataSourceType.SLAVE2)
    public String getNewBatch() {
        return carMapper.getNewBatch();
    }

    public int updateCar(TbTransportDriverCar tbTransportDriverCar){
        return tbTransportDriverCarMapper.updateCar(tbTransportDriverCar);
    }

    @Override
    public List<TbTransportDriverCar> selectIdentifyCarList(TbTransportDriverCar tbTransportDriverCar){
//        List<TbTransportDriverCar> list = tbTransportDriverCarMapper.selectIdentifyCarList(tbTransportDriverCar);
//        for (int i=0;i<list.size();i++){
//            if (StringUtils.isBlank(list.get(i).getModel())){
//                list.remove(list.get(i));
//                --i;
//            }
//        }
        return tbTransportDriverCarMapper.selectIdentifyCarList(tbTransportDriverCar);
    }

    @Override
    public TbTransportDriverCar selectIdentifyCarById(Long carId) {
        return tbTransportDriverCarMapper.selectIdentifyCarById(carId);
    }

    @Override
    public int updateModel(String model, String vin,String idCard, String carNo) {
        return tbTransportDriverCarMapper.updateModel(model,vin,idCard,carNo);
    }

}
